#include <iostream>
#include <stdio.h>
using namespace std;
#define N 100
struct Student
{
	int num;
	string name;
	int grade;
};
struct SqList
{
	Student* elem;		// 顺序表的存储空间的基地址 
	int length;		// 元素个数 

};		// 顺序表 

bool InitList(SqList &L)
{
	L.elem = new Student[N];		// 开辟数组空间 
	if(!L.elem) return false;
	L.length = 0;	
//	delete []L.elem;			// 初始化元素个数
	return true; 
}
//判断函数是否满了的功能
bool IsFull(SqList L)
{
	if(L.length == N) return true;
	else return false;
}
// 插入元素，i为插入的位置 [1, len+1] , e 为插入的元素 
bool InsertList(SqList &L, int i, Student e)
{
	if(i < 1 || i > L.length + 1 || IsFull(L))	return false;
	for(int j = L.length - 1; j >= i - 1; --j)
		L.elem[j + 1] = L.elem[j];
	L.elem[i - 1] = e;
	L.length++;//每次往表里面插一个元素，length+1;
	return true;
}
//头插
bool InsertFirst(SqList &L, Student e)
{
	return InsertList(L, 1, e);
	}
//尾插
bool InsertLast(SqList &L, Student e)
{
	return InsertList(L, L.length + 1,e);
	}
// 取值 a(i) i[1, len]
bool GetElem(SqList L, int i, Student &e)
{
	if(i < 1 || i > L.length)	return false;
	e =  L.elem[i-1];
	return true;
}
//排序函数(方法使用的是选择排序)
bool Mysort(SqList &L,int n)
{
	if(n>N) return false;
	Student t;
for(int i=0;i<n-1;i++){
	for(int j=i+1;j<n;j++){
		if(L.elem[i].grade<L.elem[j].grade){
			t=L.elem[i];
			L.elem[i]=L.elem[j];
			L.elem[j]=t;		
		}
	}
}
}


	


//删除功能
//i-->[1,len]
bool Del(SqList &L,int i)
{
	if((i<1)||(i>L.length)){
		return false;   //i的值不合法
	}
for(int j=i;j<L.length-1;j++){
	L.elem[j-1]=L.elem[j];
}
L.length--;
return true;
	}
	
//写一个专门打印线性表内结构的功能(遍历函数)
void Myprint(SqList L,int len)
{
	for(int i = 1; i <=len; i++)
			{
				Student Stuprint;//定义一个储存打印结构
				GetElem(L, i, Stuprint);
				cout << Stuprint.num<< "      " << Stuprint.name << "      " << Stuprint.grade << endl;	
			}		
			printf("\n");
			cout << "List length is " << len << endl;	
	     printf("\n");
}
//查找功能(按学号查找)   ElemType e
bool LocateElem(SqList L,int num)
{
	for(int i=0;i<L.length;i++){
		if(L.elem[i].num==num){
			printf("找到了！\n");
				cout << L.elem[i].num<< "      " <<L.elem[i].name << "      " << L.elem[i].grade << endl;
				return true;	
		}
	}
	return false;
}












int main()
{
	//定义一个结构体数组，并对此进行赋值
Student students[10]={
	{1,"a",80},
	{2,"b",70},
	{3,"c",100},
	{4,"d",100},
	{5,"e",99},
	{6,"f",90},
	{7,"g",91},
	{8,"h",60},
	{9,"i",88},
   {10,"j",66}
   };	//定义一个学生结构体数组
	SqList list;	// 定义一个名为 list 的顺序表 
		InitList(list);//开辟线性表
		printf("结构体数组的基地址为：\n");
	cout << list.elem << endl;
	printf("线性表长度为：\n");
	cout << "List length is " << list.length << endl;
	
	
	printf("插入数据之后:\n");
		for(int i = 1; i <= 10; i++)
			InsertList(list, i, students[i-1]);
	

	//遍历打印出来看
		Myprint(list,list.length);
	
	
	//按成绩排序	
	printf("按成绩排序之后:\n");
		Mysort(list,list.length);	
		Myprint(list,list.length);

//delete []list.elem;

	//删除某个元素
	printf("删除某个元素之后：\n")	;	
	Del(list,10);		
		Myprint(list,list.length);	
				
//查询学号为5学生成绩信息
LocateElem(list,5);
	return 0;
}
